<template>
  <div class="app-container">
    <SearchForm
      v-show="showSearch"
      v-model="queryParams"
      :options="options.formOps"
      @onQuery="getList"
    >
    </SearchForm>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['temporary:staff:account:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <ImportExcel
          type="primary"
          full-path="/temporary/staff/account/importData"
          v-hasPermi="['temporary:staff:account:importData']"
          plain
          icon="Plus"
          @success="getList"
        ></ImportExcel>
      </el-col>
      <el-col :span="1.5">
        <ExportExcel
          type="primary"
          full-path="/temporary/staff/account/importTemplate"
          v-hasPermi="['temporary:staff:account:importTemplate']"
          plain
        ></ExportExcel>
      </el-col>
      <el-col :span="1.5">
        <ImportExcel
          type="primary"
          full-path="/temporaryStaffAccountCertificate/importData"
          plain
          icon="Plus"
          @success="getList"
          >导入证书</ImportExcel
        >
      </el-col>
      <el-col :span="1.5">
        <ExportExcel
          type="primary"
          full-path="/temporaryStaffAccountCertificate/importTemplate"
          plain
          >下载证书模板</ExportExcel
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <Table
      ref="table"
      :auto-featch="true"
      :fetch-data="fetchData"
      :options="options.table"
      :actionWidth="280"
      :rowClassName="rowClassName"
    >
      <template #personType="{ row }"
        >{{ row.personType == "1" ? "外来访客" : "承包商" }}
      </template>
      <template #actionButton="{ row }">
        <el-button type="text" @click="handleDetail(row)">证书信息</el-button>
        <el-button
          type="text"
          icon="Edit"
          @click="handleEdit(row)"
          v-hasPermi="['temporary:staff:account:edit']"
          >修改</el-button
        >
        <el-button
          type="text"
          :disabled="row.outFactoryTime"
          @click="handleLevel(row)"
          v-hasPermi="['temporary:staff:account:departure']"
          >确认离场</el-button
        >
        <el-button
          type="text"
          icon="Delete"
          style="color: red"
          @click="handleDel(row)"
          v-hasPermi="['temporary:staff:account:delete']"
          >删除</el-button
        >
      </template></Table
    >
    <DialogVform
      :formJson="formJson"
      ref="dialogVform"
      @onSubmit="onSubmit"
      :title="dialogType == 'add' ? '新增临时人员' : '修改临时人员'"
      :slots="['fileSlot']"
    >
      <template #fileSlot>
        <el-form-item label="证照附件">
          <FileUpload
            :limit="1"
            :fileSize="30"
            :fileType="['doc', 'docx', 'pdf']"
            v-model="files"
          ></FileUpload>
        </el-form-item> </template
    ></DialogVform>
    <el-dialog v-model="outerVisible" width="90%" title="证书信息">
      <Inner ref="inner" :parentNode="parentNode"></Inner>
    </el-dialog>
  </div>
</template>

<script setup>
import Table from "@/components/Table/index.vue";
import SearchForm from "@/components/searchForm/index";
import DialogVform from "@/components/dialogVform/index";
import { apis as APIS } from "@/api/aqsc/xgfgl/lssgrytz.js";
import formJson from "./components/options.json";
import { useAsyncData, useFormJson } from "@/views/hooks/useAsyncData.js";
import { parseTime } from "@/utils/ruoyi.js";
import ImportExcel from "@/components/ImportExcel/index.vue";
import ExportExcel from "@/components/ExportExcel/index.vue";
import Inner from "./components//inner.vue";

const { proxy } = getCurrentInstance();

const handleLevel = (row) => {
  proxy.$modal.confirm("是否确认离场 ？").then(function () {
    APIS.update({
      ...row,
      outFactoryTime: parseTime(new Date()),
    }).then(() => {
      proxy.$modal.msgSuccess("离场成功");
      getList();
    });
  });
};
const ops = useFormJson(formJson, {
  sort: [],
  sortable: [],
});
const options = reactive({
  table: ops,
  formOps: ops.filter((i) =>
    [
      "name",
      "inFactoryTime",
      "outFactoryTime",
      "visitDepartment",
      "personCharge",
      "company",
      "company",
      "certificates",
    ].includes(i.prop)
  ),
});
const showSearch = ref(true);
const dialogVform = ref();
const table = ref();
const files = ref([]);
watch(
  () => files,
  (newData) => {
    if (newData.value.length === 0) {
      form.value.fileName = "";
      form.value.fileId = "";
      form.value.fileUrl = "";
    } else {
      form.value.fileName = newData.value[0].name;
      form.value.fileId = newData.value[0].id;
      form.value.fileUrl = newData.value[0].url;
    }
  },
  {
    deep: true,
  }
);
const plugEdit = (row) => {
  if (row.fileId) {
    files.value = [
      {
        name: row.fileName,
        id: row.fileId,
        url: row.fileUrl,
      },
    ];
  } else {
    files.value = [];
  }
};
const {
  onSubmit,
  fetchData,
  handleAdd,
  handleEdit,
  getList,
  handleDel,
  queryParams,
  dialogType,
  form,
} = useAsyncData({ APIS, formJson, dialogVform, table, proxy, plugEdit });

const rowClassName = ({ row }) => {
  const nowTime = Date.now();
  const endTime = new Date(
    row.outFactoryTime || row.planOutFactoryTime
  ).getTime();
  if (endTime && nowTime >= endTime) {
    return "deepred";
  } else {
    return "";
  }
};

// 类型
const inner = ref();
const parentNode = ref({});
const outerVisible = ref(false);
const handleDetail = (row) => {
  outerVisible.value = true;
  nextTick().then(() => {
    parentNode.value = row;
  });
};
</script>

<style lang="scss" scoped></style>
